#!/usr/bin/env python3
# -*- coding: utf-8 -*-
__author__ = "jzp";

import requests;
from bs4 import BeautifulSoup;
import re;
import os;

'''
冒牌大英雄
http://www.sfac.xyz/library/novel/31
'''

headers = {
    "User-Agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:109.0) Gecko/20100101 Firefox/113.0",
    "Token":"eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJhdWQiOiIxIiwicm9sZSI6IkFkbWluIiwidXNlckltYWdlIjoiaHR0cDovL3d3dy5zZmFjLnh5ejo4MDAwL2ltYWdlcy9wcm9maWxlLzE2NzAwNzA0NzMwNTUuanBnIiwiaWQiOjEsImV4cCI6MTY4Mzg5ODk2OCwidXNlck5hbWUiOiJhZG1pbiIsImlhdCI6MTY4MzgxMjU2OH0.jdx_Y31cpaSRe-3TBNgAcq8gG8a9IKmQ1YYIRM-TfcQ"
};

# 处理小说主页数据
def get_novel_data(url):
    response = requests.get(url=url, headers=headers);
    if response.status_code == 200:
        response.encoding = response.apparent_encoding;
        result = response.json();

        # 创建目标文件夹
        dest_folder = "/temp";
        if not os.path.exists(dest_folder):
            os.makedirs(dest_folder);
        # 获得目标文件路劲并删除该文件
        dest_file = "%s/%s.txt" % (dest_folder, result.get("name"));
        os.remove(dest_file);

        chapters = result.get("chapters");
        chapter_url_list = list("http://538b537e25.zicp.vip:13595/api/library/chapter/%d" % item.get("id")
                                for item in chapters);
        for chapter_url in chapter_url_list:
            get_chapter_data(chapter_url, dest_file);

# 处理小说章节数据
def get_chapter_data(url, dest_file):
    response = requests.get(url=url, headers=headers);
    if response.status_code == 200:
        response.encoding = response.apparent_encoding;
        result = response.json();

        # 构造小说章节标题、内容
        chapter_name = result.get("name");
        print(chapter_name)
        lines = []
        for line in re.findall("<p>(.*?)</p>", result.get("content")):
            lines.append("%s\r\n" % line);

        # 将小说内容追加到 txt 文档
        with open(file=dest_file, mode="a", encoding="utf8") as f:
            f.write(chapter_name);
            f.write("\r\n");
            f.writelines(lines);

if __name__ == '__main__':
    url = "http://538b537e25.zicp.vip:13595/api/library/novel/31";
    get_novel_data(url);
